<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>修改</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/html/main/config/lib/main.css" media="all">
    <style>
        .layui-form-select {
            width: 100%;
        }
        
        .layui-select-title {
            width: 100%;
        }
    </style>
</head>

<body id="layui-body-content" class="layui-bg">
    <div id="layui-body-content" class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form" action="" lay-filter="layui-form-Table">
                    <table class="layui-table">
                        <tr>
                            <td width="100">
                                用户名
                            </td>
                            <td>
                                <input type="text" name="username" autocomplete="off" class="layui-input" disabled>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                真实姓名
                            </td>
                            <td>
                                <input type="text" name="truename" lay-verify="required" autocomplete="off" class="layui-input">
                            </td>
                        </tr>
                        <tr>
                            <td>
                                单位名称
                            </td>
                            <td>
                                <input type="text" id="depttree" name="depttree" lay-filter="tree" class="layui-input">
                            </td>
                        </tr>
                        <tr>
                            <td>
                                所属科室
                            </td>
                            <td>
                                <input type="text" name="remark" autocomplete="off" class="layui-input">
                            </td>
                        </tr>
                        <tr>
                            <td>
                                联系电话
                            </td>
                            <td>
                                <input type="text" name="phonenumber" autocomplete="off" class="layui-input">
                            </td>
                        </tr>
                        <tr>
                            <td>
                                电子邮箱
                            </td>
                            <td>
                                <input type="text" name="email" autocomplete="off" class="layui-input">
                            </td>
                        </tr>
                        <tr>
                            <td>
                                用户头像
                            </td>
                            <td>
                                <img id="imgurl" width="50px" height="50px" src="/res/img/zwtp.jpg">
                                <a id="uploadtp" href="javascript:void(0);" style="color: blue;">点击上传图片</a>
                            </td>
                        </tr>
                    </table>
                    <div class="layui-form-item">
                        <div class="layui-input-block layui-hide">
                            <input type="hidden" name="id" value="0">
                            <input type="hidden" name="type" value="0">
                            <input type="hidden" name="password">
                            <input type="hidden" name="fkid">
                            <input type="hidden" name="deptid" id="deptid">
                            <input type="hidden" name="deptname">
                            <input type="hidden" name="imgurl">
                            <input type="hidden" name="isenabled">
                            <input type="hidden" name="errorcount">
                            <button class="layui-btn" lay-submit lay-filter="YjbtnSave" id="YjbtnSave" type="button">提交</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <script src="/layui/layui.js"></script>
    <script>
        layui.extend({
            treeSelect: '/res/js/extend/treeSelect/treeSelect',
            main: '../../main/config/lib/main'
        }).define(['form', 'jquery', 'layer', 'main', 'treeSelect', 'upload', 'jm'], function(exports) {
            var form = layui.form,
                $ = layui.jquery,
                layer = layui.layer,
                yjUrl = parent.layui.yjUrl,
                jm = layui.jm,
                admin = parent.layui.admin,
                main = layui.main,
                treeSelect = layui.treeSelect,
                upload = layui.upload;

            var uid = layui.data(parent.layui.setter.tableName)['yj_KeyID'];

            //自定义验证规则
            form.verify({
                dhhm: function(value) {
                    if (value != "") {
                        if (!/^((0\d{2,3}-\d{7,8})|(1[345678]\d{9}))$/.test(value)) {
                            return '请输入正确的联系电话';
                        }
                    }
                },
                emailyz: function(value) {
                    if (value != "") {
                        if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$$/.test(value) && !/^0\d{2,3}-?\d{7,8}$/.test(value)) {
                            return '请输入正确的电子邮箱';
                        }
                    }
                }
            });

            //赋值
            parent.layui.admin.req({
                url: 'system/users/Edit',
                type: 'get',
                data: {
                    id: uid
                },
                done: function(res) {
                    if (res.successful) {
                        res.data = JSON.parse(jm.s4.decryptData_ECB(res.data));
                        form.val("layui-form-Table", {
                            "id": res.data.id,
                            "username": res.data.username,
                            "password": res.data.password,
                            "truename": res.data.truename,
                            "deptid": res.data.deptid,
                            "deptname": res.data.deptname,
                            "phonenumber": res.data.phonenumber,
                            "email": res.data.email,
                            "areanumber": res.data.areanumber,
                            "type": res.data.type,
                            "fkid": res.data.fkid,
                            "remark": res.data.remark,
                            "imgurl": res.data.imgurl,
                            "isenabled": res.data.isenabled,
                            "errorcount": res.data.errorcount
                        });
                        if (res.data.imgurl != '') {
                            var host_url = yjUrl.find('/').urlPath;
                            var host = host_url.substring(0, host_url.indexOf("api") - 1);
                            $("#imgurl").attr('src', host + res.data.imgurl);
                        }
                        treeSelect.render({
                            // 选择器
                            elem: '#depttree',
                            headers: main.headerToken(),
                            // 数据
                            data: yjUrl.find('system/dept/GetDeptList').urlPath,
                            // 异步加载方式：get/post，默认get
                            type: 'get',
                            // 占位符
                            placeholder: '请选择部门',
                            // 是否开启搜索功能：true/false，默认false
                            search: true,
                            // 点击回调
                            click: function(d) {
                                $("input[name='deptid']").val(d.current.deptid);
                                $("input[name='deptname']").val(d.current.deptname);
                            },
                            // 加载完成后的回调函数
                            success: function(d) {
                                if (res.data.deptid != '') {
                                    //选中节点，根据id筛选
                                    treeSelect.checkNode('tree', res.data.deptid);
                                    //获取zTree对象，可以调用zTree方法
                                    var treeObj = treeSelect.zTree('tree');
                                }
                            }
                        });
                    }
                }
            });

            //上传用户图片
            upload.render({
                elem: '#uploadtp',
                headers: main.headerToken(),
                url: yjUrl.find('System/Users/AddUserIMG').urlPath + '?id=' + uid,
                done: function(res) {
                    parent.layer.msg(res.msg);
                    if (res.successful) {
                        res.data = JSON.parse(jm.s4.decryptData_ECB(res.data));
                        var host_url = yjUrl.find('/').urlPath;
                        var host = host_url.substring(0, host_url.indexOf("api") - 1);
                        $("#imgurl").attr('src', host + res.data);
                    }
                }
            });

        });
    </script>

</body>

</html>